<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="__ROOT__/static/plugs/jquery/jquery.min.js"></script>
    <script src="__ROOT__/static/plugs/layui/layui.js"></script>
    <style>
        .alls {
            width: 100%;
        }

        .banner {
            width: 100%;
            padding-top: 10vh;
            text-align: center;
        }

        .banner img {
            width: 80%;
        }

        .handerBen {
            padding-top: 10vh;
            text-align: center;
        }

        .handerBen button {
            background-color: #009987;
            background: #009987;
            border: none;
            border-radius: 5px;
            width: 93%;
            height: 50px;
            color: #fff;
            font-size: 16px;
        }
    </style>
    <title>授权</title>
</head>
<body>
<div class="alls">
    <div class="banner">
        <img src="__ROOT__/static/theme/img/auth_bg.png" alt="">
    </div>
    <div class="handerBen">
        <button id="btn">立即授权</button>
    </div>
</div>
</body>
</html>
<script>
    $(function () {
        var query = window.location.search.substring(1);
        var id = getParams(query, 'id');
        if (id) {
            var redirect_uri = 'http://huawei.9999jt.com/admin/shopAdmin/auth?newid=' + id;
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx421cc73af8168c84&redirect_uri='+ redirect_uri +'&response_type=code&scope=snsapi_base';
        }

        var code = getParams(query, 'code');
        if (code) {
            $('#btn').click(function () {
                $.ajax({
                    url: "{:url('admin/ShopAdmin/getOpenid')}",
                    data: {
                        code: code,
                        id: getParams(query, 'newid')
                    },
                    type: 'get',
                    success: res => {
                         layer.msg(res.msg);
                    }
                })
            })
        }

    });

    /**
     * 获取url code
     * @param query
     * @param value
     * @returns {*}
     */
    function getParams(query, value) {
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == value) {
                return pair[1];
            }
        }
        return false;
    }
</script>